package com.rbac.api.service.impl;


import com.rbac.api.constant.AdminConstant;
import com.rbac.api.controller.enums.HintMessageEunm;
import com.rbac.api.controller.enums.SaveStatusTypeEnum;
import com.rbac.api.controller.request.PostMenuAddRequest;
import com.rbac.api.controller.request.PostMenuSaveStatusRequest;
import com.rbac.api.controller.request.PostMenusListRequest;
import com.rbac.api.controller.response.Response;
import com.rbac.api.mybatis.entity.AdminMenusEntity;
import com.rbac.api.mybatis.entityCus.CusHaveMenusEntity;
import com.rbac.api.mybatis.mapper.AdminMenusMapper;
import com.rbac.api.service.MenusCommonService;
import com.rbac.api.service.common.AdminCommonService;
import com.rbac.api.service.common.InsActionLogService;
import com.rbac.api.service.common.SaveStatusCommService;
import com.rbac.api.utils.ApiPublicUtil;
import com.rbac.utils.commUtils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;


///
@Service
public class MenusCommonServiceImpl implements MenusCommonService {


    ///操作管理员token公共service
    @Autowired
    private AdminCommonService adminCommonService;

    ///菜单表
    @Autowired
    private AdminMenusMapper adminMenusMapper;

    //事务
    @Autowired
    private TransactionService transactionService;

    ///记录操作日志service
    @Autowired
    private InsActionLogService insActionLogService;

    //日志操作
    private static final LogUtil logger = LogUtil.getLogger(MenusCommonServiceImpl.class);

    ///修改状态公共service
    @Autowired
    private SaveStatusCommService saveStatusCommService;


    ///获取拥有的菜单列表
    @Override
    public Object getHaveListByTokenService(HttpServletRequest request) throws Exception {
        ///获取管理员编号
        String userNumber = adminCommonService.getUserNoByRequestHeader(request);
        ///
        List<CusHaveMenusEntity> menusList = adminMenusMapper.getHaveMenusListUserNoSelective(userNumber);


        ///如果没有菜单，请联系管理员分配权限
        if(menusList.size() == 0){
            return new Response(HintMessageEunm.EXCEPTION_NOT_HAVE_ANY_MENU_POWER_ERROR,new String[]{});
        }

        ///返回菜单list
        return new Response(HintMessageEunm.EXCEPTION_SUCCESS_DEFAULT, ApiPublicUtil.getMenusSon(menusList, AdminConstant.REPRESENT_PARENT_NO_VALUE));
    }


    ///菜单列表
    @Override
    public Object getMenusListService(PostMenusListRequest menusListRequest) throws Exception {
        //数据总条数
        Integer countDataNum = adminMenusMapper.getMenusListCountSelective(menusListRequest);
        //没有数据则返回空数据
        if(countDataNum.equals(AdminConstant.DEFAULT_NO_DATA_VALUE_NUMBER)){
            return new Response(HintMessageEunm.EXCEPTION_SUCCESS_DEFAULT,new String[]{});
        }

        //当前页
        Integer pageIndex = menusListRequest.getPageIndex();
        //页面显示条数
        Integer pageSize = menusListRequest.getPageSize();

        PagenationUtil pageClass = new PagenationUtil(pageSize,pageIndex,countDataNum);

        //获取页面数据
        List<AdminMenusEntity> nowPageBasicsData = adminMenusMapper.getMenusListDataSelective(menusListRequest,pageClass.getStartRow(),pageSize);

        ///
        return new Response(HintMessageEunm.EXCEPTION_SUCCESS_DEFAULT, ApiPublicUtil.AssemblyReturnItem(countDataNum,pageIndex,pageSize,nowPageBasicsData));
    }

    ///新增菜单（增加）
    @Override
    public Object menuAddService(PostMenuAddRequest menuAddRequest, HttpServletRequest request) throws Exception {
        String actionNameStr = "新增菜单";
        String actionUid = adminCommonService.getUserNoByRequestHeader(request);
        String actionAccount = adminCommonService.getUserInfoFieldByRequestHeader(request,"account");
        String menuNo = RandomStringUtil.getUUID();///管理员编号
        String strTime = ToolUtil.getStringTodayTime();


        ///菜单信息实体
        AdminMenusEntity menuOne = new AdminMenusEntity();
        menuOne.setMenuNo(menuNo);///菜单编号
        menuOne.setParentNo(menuAddRequest.getParentNo());///菜单父级编号
        menuOne.setMenuName(menuAddRequest.getMenuName());///菜单名称
        menuOne.setMenuAddress(menuAddRequest.getMenuAddress());///菜单地址
        menuOne.setMenuSort(Long.valueOf(menuAddRequest.getMenuSort()));///菜单排序
        menuOne.setCreateUid(actionUid);///创建人编号
        menuOne.setCreateTime(ToolUtil.getDateTimeByStringTime(strTime));///创建时间


        //开启事务
        TransactionStatus transactionStatus = transactionService.begin();

        try{
            adminMenusMapper.menuAddMapper(menuOne);
            insActionLogService.InsOneActionLogService(actionNameStr,actionUid, ToolUtil.getClientIp(request),"管理员 {0} 在 {1} 进行了{2}操作，产生的数据id为{3}",actionAccount, strTime,actionNameStr,menuOne.getId());
        }catch (Exception e){
            //LOG
            logger.error(actionNameStr+"："+e);
            //回滚事务
            transactionService.rollback(transactionStatus);
            //提示出现异常
            return new Response(HintMessageEunm.EXCEPTION_ACTION_ABNORMAL_ERROR);
        }

        //提交事务
        transactionService.commit(transactionStatus);
        //操作成功
        return new Response(HintMessageEunm.EXCEPTION_SUCCESS_DEFAULT);
    }


    ///新增菜单（获取顶级菜单列表）
    @Override
    public Object getTopMenuListService() throws Exception {
        List<AdminMenusEntity> topMenuList = adminMenusMapper.getTopMenuListMapper();
        return new Response(HintMessageEunm.EXCEPTION_SUCCESS_DEFAULT,topMenuList);
    }




    ///修改菜单状态
    @Override
    public Object menuSaveStatusService(PostMenuSaveStatusRequest menuSaveStatusRequest, HttpServletRequest request) throws Exception {
        String actionNameStr = "修改菜单状态";
        String nowUserNo = adminCommonService.getUserInfoFieldByRequestHeader(request,"userno");
        String actionAccount = adminCommonService.getUserInfoFieldByRequestHeader(request,"account");

        Integer id = menuSaveStatusRequest.getId();
        Integer menuStatus = menuSaveStatusRequest.getMenuStatus();
        Integer nowStatus = menuSaveStatusRequest.getNowStatus();
        Date t = ToolUtil.getTodayTime();


        return this.saveStatusCommService.common(SaveStatusTypeEnum.MENU_TABLE_SAVE_STATUS.getValue(),actionNameStr,actionAccount,menuStatus,nowUserNo,t,id,nowStatus,request);
    }





}
